﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace SolutionGenerator
{
    public static class CommonFunction
    {
        public static string StandarConfigFile
        {
            get
            {
                return string.Format("{0}\\SavedConfig.xml", Environment.CurrentDirectory);
            }
        }
        /// <summary>
        /// Gets the last x-<paramref name="amount"/> of characters from the given string.
        /// If the given string's length is smaller than the requested <see cref="amount"/> the full string is returned.
        /// If the given <paramref name="amount"/> is negative, an empty string will be returned.
        /// </summary>
        /// <param name="string">The string from which to extract the last x-<paramref name="amount"/> of characters.</param>
        /// <param name="amount">The amount of characters to return.</param>
        /// <returns>The last x-<paramref name="amount"/> of characters from the given string.</returns>
        public static string GetLast(this string @string, int amount)
        {
            if (@string == null)
            {
                return @string;
            }

            if (amount < 0)
            {
                return String.Empty;
            }

            if (amount >= @string.Length)
            {
                return @string;
            }
            else
            {
                return @string.Substring(@string.Length - amount);
            }
        }
    }
    public class SerializeProcessing<T> : IDisposable where T : class
    {
        public T DataToSerialize { get; set; }
        public SerializeProcessing() { }
        public SerializeProcessing(T data)
        {
            DataToSerialize = data;
        }
        public string Serialize
        {
            get
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb);
                dcs.WriteObject(writer, DataToSerialize);
                writer.Close();
                return sb.ToString();
            }
        }
        public T Deserialize(string textSerialize)
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            StringReader xmlString = new StringReader(textSerialize);
            using (XmlReader reader = XmlReader.Create(xmlString))
                return dcs.ReadObject(reader) as T;
        }
        public void Dispose() { }
    }
}